package bb.lanxing.lib.devices.sprint.model.impl;

import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.text.TextUtils;
import android.util.Pair;
import bb.lanxing.lib.devices.core.ble.SpecificationAttributes;
import bb.lanxing.lib.devices.core.utils.CharacteristicValueHelper;
import bb.lanxing.lib.devices.sprint.Command;
import bb.lanxing.lib.devices.sprint.Commands;
import bb.lanxing.lib.devices.sprint.Response;
import bb.lanxing.lib.devices.sprint.SprintListener;
import bb.lanxing.lib.devices.sprint.XossDevice;
import bb.lanxing.lib.devices.sprint.XossDeviceController;
import bb.lanxing.lib.devices.sprint.model.SprintHomeModel;
import bb.lanxing.lib.devices.sprint.utils.DeviceControllerHelper;
import bb.lanxing.lib.devices.utils.ByteUtils;
import bb.lanxing.lib.devices.utils.DeviceVersionHelper;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.subjects.BehaviorSubject;

public class SprintHomeModelImpl implements SprintHomeModel, SprintListener {
    private String address;
    private int battery;
    private BehaviorSubject<Integer> batterySubject;
    private CharacteristicValueHelper characteristicValueHelper;
    private Context context;
    private XossDeviceController controller;
    private BehaviorSubject<Byte> deviceStatusSubject;
    private String firmwareVersion;
    private HashMap<String, String> firmwareVersionMap;
    private BehaviorSubject<String> firmwareVersionSubject;
    private BehaviorSubject<String> hardwareVersionSubject;
    private BehaviorSubject<String> manufacturerSubject;
    private BehaviorSubject<Pair<Long, Long>> memoryInfoSubject;
    private BehaviorSubject<String> modelSubject;
    private BehaviorSubject<String> serialSubject;
    private BehaviorSubject<String> softwareVersionSubject;

    @Override
    public void connectDevice(String str, String str2, String str3, String str4, String str5, String str6, String str7, String str8) throws IOException {
    }

    @Override
    public void disconnectDevice(String str, String str2, String str3) throws IOException {
    }

    protected int getSupportedVersion() {
        return 1;
    }

    @Override
    public void onCmdStatus(Command command, int i, byte[] bArr) {
    }

    @Override
    public void onFileReceived(String str) {
    }

    @Override
    public void onProgressUpdate(Command command, float f) {
    }

    public SprintHomeModelImpl(Context context) {
        this.context = context;
        this.characteristicValueHelper = new CharacteristicValueHelper(context, new CharacteristicValueHelper.OnCharacteristicValueChangedListener() {
            @Override
            public void onCharacteristicValue(CharacteristicValueHelper characteristicValueHelper, String str, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
                UUID uuid = bluetoothGattCharacteristic.getUuid();
                if (SpecificationAttributes.BLE_BATTERY_CHARACTERISTIC.equals(uuid)) {
                    SprintHomeModelImpl.this.battery = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
                    if (SprintHomeModelImpl.this.batterySubject != null) {
                        SprintHomeModelImpl.this.batterySubject.onNext(SprintHomeModelImpl.this.battery);
                    }
                } else if (SpecificationAttributes.BLE_DEVICE_INFORMATION_FIRMWARE_CHARACTERISTIC.equals(uuid)) {
                    String stringValue = bluetoothGattCharacteristic.getStringValue(0);
                    DeviceVersionHelper.setLocalVersion(SprintHomeModelImpl.this.context, str, stringValue);
                    SprintHomeModelImpl.this.firmwareVersion = stringValue;
                    if (SprintHomeModelImpl.this.firmwareVersionMap != null) {
                        SprintHomeModelImpl.this.firmwareVersionMap.put(str, SprintHomeModelImpl.this.firmwareVersion);
                    } else {
                        SprintHomeModelImpl.this.firmwareVersionMap = new HashMap();
                        SprintHomeModelImpl.this.firmwareVersionMap.put(str, SprintHomeModelImpl.this.firmwareVersion);
                    }
                    if (SprintHomeModelImpl.this.firmwareVersionSubject != null) {
                        SprintHomeModelImpl.this.firmwareVersionSubject.onNext(SprintHomeModelImpl.this.firmwareVersion);
                    }
                } else if (SpecificationAttributes.BLE_DEVICE_INFORMATION_SOFTWARE_CHARACTERISTIC.equals(uuid)) {
                    String stringValue2 = bluetoothGattCharacteristic.getStringValue(0);
                    if (SprintHomeModelImpl.this.softwareVersionSubject != null) {
                        SprintHomeModelImpl.this.softwareVersionSubject.onNext(stringValue2);
                    }
                } else if (SpecificationAttributes.BLE_DEVICE_INFORMATION_HARDWARE_CHARACTERISTIC.equals(uuid)) {
                    String stringValue3 = bluetoothGattCharacteristic.getStringValue(0);
                    if (SprintHomeModelImpl.this.hardwareVersionSubject != null) {
                        SprintHomeModelImpl.this.hardwareVersionSubject.onNext(stringValue3);
                    }
                } else if (SpecificationAttributes.BLE_DEVICE_INFORMATION_MODEL_CHARACTERISTIC.equals(uuid)) {
                    String stringValue4 = bluetoothGattCharacteristic.getStringValue(0);
                    if (SprintHomeModelImpl.this.modelSubject != null) {
                        SprintHomeModelImpl.this.modelSubject.onNext(stringValue4);
                    }
                } else if (SpecificationAttributes.BLE_DEVICE_INFORMATION_SERIAL_CHARACTERISTIC.equals(uuid)) {
                    String stringValue5 = bluetoothGattCharacteristic.getStringValue(0);
                    if (SprintHomeModelImpl.this.serialSubject != null) {
                        SprintHomeModelImpl.this.serialSubject.onNext(stringValue5);
                    }
                } else if (SpecificationAttributes.BLE_DEVICE_INFORMATION_MANUFACTURER_CHARACTERISTIC.equals(uuid)) {
                    String stringValue6 = bluetoothGattCharacteristic.getStringValue(0);
                    if (SprintHomeModelImpl.this.manufacturerSubject != null) {
                        SprintHomeModelImpl.this.manufacturerSubject.onNext(stringValue6);
                    }
                }
            }
        });
    }

    @Override
    public void setAddress(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        this.firmwareVersion = null;
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController != null) {
            xossDeviceController.unregisterXossDeviceListener(this);
        }
        this.address = str;
        XossDeviceController xossDeviceController2 = DeviceControllerHelper.getXossDeviceController(str);
        this.controller = xossDeviceController2;
        if (xossDeviceController2 != null) {
            xossDeviceController2.registerXossDeviceListener(this);
        }
        requestFirmwareVersion();
    }

    @Override
    public void subscribeHardwareVersion(Action1<String> action1) {
        if (this.hardwareVersionSubject == null) {
            this.hardwareVersionSubject = BehaviorSubject.create();
        }
        this.hardwareVersionSubject.observeOn(AndroidSchedulers.mainThread()).subscribe(action1);
    }

    @Override
    public void subscribeModel(Action1<String> action1) {
        if (this.modelSubject == null) {
            this.modelSubject = BehaviorSubject.create();
        }
        this.modelSubject.observeOn(AndroidSchedulers.mainThread()).subscribe(action1);
    }

    @Override
    public void subscribeSerial(Action1<String> action1) {
        if (this.serialSubject == null) {
            this.serialSubject = BehaviorSubject.create();
        }
        this.serialSubject.observeOn(AndroidSchedulers.mainThread()).subscribe(action1);
    }

    @Override
    public void subscribeManufacturer(Action1<String> action1) {
        if (this.manufacturerSubject == null) {
            this.manufacturerSubject = BehaviorSubject.create();
        }
        this.manufacturerSubject.observeOn(AndroidSchedulers.mainThread()).subscribe(action1);
    }

    @Override
    public void subscribeFirmwareVersion(Action1<String> action1) {
        if (this.firmwareVersionSubject == null) {
            this.firmwareVersionSubject = BehaviorSubject.create();
        }
        this.firmwareVersionSubject.observeOn(AndroidSchedulers.mainThread()).subscribe(action1);
    }

    @Override
    public void subscribeBattery(Action1<Integer> action1) {
        if (this.batterySubject == null) {
            this.batterySubject = BehaviorSubject.create();
        }
        this.batterySubject.observeOn(AndroidSchedulers.mainThread()).subscribe(action1);
    }

    @Override
    public void subscribeSoftwareVersion(Action1<String> action1) {
        if (this.softwareVersionSubject == null) {
            this.softwareVersionSubject = BehaviorSubject.create();
        }
        this.softwareVersionSubject.observeOn(AndroidSchedulers.mainThread()).subscribe(action1);
    }

    @Override
    public void subscribeMemoryRemaining(Action1<Pair<Long, Long>> action1) {
        if (this.memoryInfoSubject == null) {
            this.memoryInfoSubject = BehaviorSubject.create();
        }
        this.memoryInfoSubject.observeOn(AndroidSchedulers.mainThread()).subscribe(action1);
    }

    @Override
    public void subscribeDeviceStatus(Action1<Byte> action1) {
        if (this.deviceStatusSubject == null) {
            this.deviceStatusSubject = BehaviorSubject.create();
        }
        this.deviceStatusSubject.observeOn(AndroidSchedulers.mainThread()).subscribe(action1);
    }

    @Override
    public void requestFirmwareVersion() {
        CharacteristicValueHelper.readCharacteristicValue(this.context, this.address, SpecificationAttributes.BLE_DEVICE_INFORMATION_FIRMWARE_CHARACTERISTIC);
    }

    @Override
    public void requestHardwareVersion() {
        CharacteristicValueHelper.readCharacteristicValue(this.context, this.address, SpecificationAttributes.BLE_DEVICE_INFORMATION_HARDWARE_CHARACTERISTIC);
    }

    @Override
    public void requestManufacturer() {
        CharacteristicValueHelper.readCharacteristicValue(this.context, this.address, SpecificationAttributes.BLE_DEVICE_INFORMATION_MANUFACTURER_CHARACTERISTIC);
    }

    @Override
    public void requestModel() {
        CharacteristicValueHelper.readCharacteristicValue(this.context, this.address, SpecificationAttributes.BLE_DEVICE_INFORMATION_MODEL_CHARACTERISTIC);
    }

    @Override
    public void requestSerial() {
        CharacteristicValueHelper.readCharacteristicValue(this.context, this.address, SpecificationAttributes.BLE_DEVICE_INFORMATION_SERIAL_CHARACTERISTIC);
    }

    @Override
    public void requestBattery() {
        CharacteristicValueHelper.readCharacteristicValue(this.context, this.address, SpecificationAttributes.BLE_BATTERY_CHARACTERISTIC);
    }

    @Override
    public void requestSoftwareVersion() {
        CharacteristicValueHelper.readCharacteristicValue(this.context, this.address, SpecificationAttributes.BLE_DEVICE_INFORMATION_SOFTWARE_CHARACTERISTIC);
    }

    @Override
    public void requestMemoryRemaining() {
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController instanceof XossDevice) {
            ((XossDevice) xossDeviceController).post(new Runnable() {
                @Override
                public void run() {
                    try {
                        Response execute = SprintHomeModelImpl.this.controller.execute(Commands.create(9, null));
                        if (execute.getCmd() != 10) {
                            execute = SprintHomeModelImpl.this.controller.getResponse();
                        }
                        String[] split = new String(execute.getContent()).split("/");
                        long parseLong = Long.parseLong(split[0]);
                        long parseLong2 = Long.parseLong(split[1]);
                        if (SprintHomeModelImpl.this.memoryInfoSubject != null) {
                            SprintHomeModelImpl.this.memoryInfoSubject.onNext(new Pair(parseLong2, parseLong));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Override
    public void requestDeviceStatus() {
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController instanceof XossDevice) {
            ((XossDevice) xossDeviceController).post(new Runnable() {
                @Override
                public void run() {
                    try {
                        Response execute = SprintHomeModelImpl.this.controller.execute(Commands.create(-1, null));
                        if (SprintHomeModelImpl.this.deviceStatusSubject != null) {
                            SprintHomeModelImpl.this.deviceStatusSubject.onNext(execute.getCmd());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Override
    public void senTime() {
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController instanceof XossDevice) {
            ((XossDevice) xossDeviceController).post(new Runnable() {
                @Override
                public void run() {
                    byte[] bArr = new byte[4];
                    ByteUtils.intToBytesLE((int) (System.currentTimeMillis() / 1000), bArr, 0);
                    try {
                        SprintHomeModelImpl.this.controller.sendCmd(Commands.create(84, bArr));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Override
    public boolean isAppVersionTooLower() {
        return checkFirmwareVersion() < 0;
    }

    @Override
    public boolean isFirmwareTooLower() {
        return checkFirmwareVersion() >= 1;
    }

    @Override
    public void release() {
        if (this.firmwareVersion != null) {
            this.firmwareVersionSubject.onCompleted();
        }
        BehaviorSubject<Integer> behaviorSubject = this.batterySubject;
        if (behaviorSubject != null) {
            behaviorSubject.onCompleted();
        }
        BehaviorSubject<Pair<Long, Long>> behaviorSubject2 = this.memoryInfoSubject;
        if (behaviorSubject2 != null) {
            behaviorSubject2.onCompleted();
        }
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController != null) {
            xossDeviceController.unregisterXossDeviceListener(this);
        }
        CharacteristicValueHelper characteristicValueHelper = this.characteristicValueHelper;
        if (characteristicValueHelper != null) {
            characteristicValueHelper.release();
            this.characteristicValueHelper = null;
        }
        this.controller = null;
        this.context = null;
    }

    @Override
    public String getFirmwareVersion() {
        return this.firmwareVersion;
    }

    @Override
    public String getFirmwareVersionWithAddress(String str) {
        HashMap<String, String> hashMap = this.firmwareVersionMap;
        if (hashMap == null || !hashMap.containsKey(str)) {
            return null;
        }
        return this.firmwareVersionMap.get(str);
    }

    public int checkFirmwareVersion() {
        Pattern compile = Pattern.compile("^V(\\d+)\\.(\\d+)");
        String str = this.firmwareVersion;
        if (str == null) {
            str = "";
        }
        Matcher matcher = compile.matcher(str);
        if (matcher.matches()) {
            return getSupportedVersion() - Integer.parseInt(matcher.group(1));
        }
        return 0;
    }

    @Override
    public void clear() {
        this.firmwareVersion = null;
    }
}
